Imports Google.GData.Calendar
Imports Google.GData.Client
Imports Google.GData.Extensions

Partial Class Calendar
    Inherits BasePage

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim theService As New CalendarService("BYUMiller Lab")
        theService.setUserCredentials("byumillerlab@gmail.com", "testing247")

        Dim myQuery As EventQuery = New EventQuery("http://www.google.com/calendar/feeds/byumillerlab@gmail.com/private/full")
        myQuery.Query = userName.Text
        Dim results As EventFeed = theService.Query(myQuery)
        Dim Whenitem As [When]

        Dim mytable As Data.DataTable = New Data.DataTable
        Dim Title As Data.DataColumn = New Data.DataColumn("Title")
        Dim Where As Data.DataColumn = New Data.DataColumn("Where")
        Dim Description As Data.DataColumn = New Data.DataColumn("Description")
        Dim Day As Data.DataColumn = New Data.DataColumn("Day")
        Dim StartTime As Data.DataColumn = New Data.DataColumn("StartTime")
        Dim EndTime As Data.DataColumn = New Data.DataColumn("EndTime")

        Dim item As EventEntry

        Title.DataType = System.Type.GetType("System.String")
        Where.DataType = System.Type.GetType("System.String")
        Description.DataType = System.Type.GetType("System.String")
        Day.DataType = System.Type.GetType("System.String")
        StartTime.DataType = System.Type.GetType("System.String")
        EndTime.DataType = System.Type.GetType("System.String")

        mytable.Columns.Add(Title)
        mytable.Columns.Add(Where)
        mytable.Columns.Add(Description)
        mytable.Columns.Add(Day)
        mytable.Columns.Add(StartTime)
        mytable.Columns.Add(EndTime)

        For Each item In results.Entries

            Dim myrow As Data.DataRow

            myrow = mytable.NewRow()

            myrow.Item("Title") = "<a href='" + item.Links.Item(0).AbsoluteUri.ToString + "' target='_blank'><u><b>" + item.Title.Text() + "</u></b></a>"

            myrow.Item("Where") = item.Locations.Item(0).ValueString

            myrow.Item("Description") = item.Content.Content.ToString

            For Each Whenitem In item.Times

                myrow.Item("Day") = Whenitem.StartTime.ToShortDateString

                myrow.Item("StartTime") = Whenitem.StartTime.ToShortTimeString

                myrow.Item("EndTime") = Whenitem.EndTime.ToShortTimeString

            Next

            mytable.Rows.Add(myrow)

            Dim myDataView As Data.DataView = mytable.DefaultView

            Datalist2.DataSource = myDataView

            Datalist2.DataBind()

            Datalist2.Visible = True
        Next

    End Sub
End Class